 /**
 * 逻辑、功能相关描述:
 *
 * @author 王浩羽 编写日期：Dec 30, 2008
 */
package com.ce.pms.occupation.dao.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import com.ce.common.util.BlankUtil;
import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.occupation.dao.IIdentityValidateDao;
import com.ce.pms.occupation.dao.IOccupationChargeItemDao;
import com.ce.pms.occupation.model.IdentityValidateDO;
import com.ce.pms.occupation.model.OccHouseClientInfoDO;
import com.ce.pms.occupation.model.OccupationChargeItemDO;
import com.ce.pms.occupation.vo.OccupationQueryConditionVO;
import com.sitechasia.webx.core.support.Page;

/**
 * 入伙身份验证DAO
 * @author 王浩羽
 *
 */
public class IdentityValidateDaoImpl extends PmsBaseDaoImpl<IdentityValidateDO> implements IIdentityValidateDao
{

	private IOccupationChargeItemDao occupationChargeItemDao;

	@SuppressWarnings("unchecked")
	public Page queryIdentityValidateList(OccupationQueryConditionVO queryCondition)
	{
		StringBuilder hqlBuilder = new StringBuilder();
		List params = new ArrayList();
		hqlBuilder.append("SELECT OBJ FROM ").append(IdentityValidateDO.class.getSimpleName()).append(" OBJ where 1=1 ");
		//楼栋ID
		Long buildingDomainId = queryCondition.getBuildingDomainId();
		if(buildingDomainId != null)
		{
			hqlBuilder.append(" AND OBJ.house.building.keyword=? ");
			params.add(buildingDomainId);
		}

		 // 房间ID(注：界面没有此条件)
		 Long houseId = queryCondition.getHouseId();
		 if(houseId!=null)
		{
			 hqlBuilder.append(" AND OBJ.house.keyword = ? ");
			 params.add(houseId);
		}

		 // 房号
		 String houseNumber = queryCondition.getHouseNumber();
		 if(StringUtils.hasText(houseNumber))
		{
			 hqlBuilder.append(" AND OBJ.house.houseNumber like ? ");
			 params.add("%"+houseNumber+"%");
		}

		//业主名称
		String ownerName = queryCondition.getOwnerName();
		if(StringUtils.hasText(ownerName)){
			 hqlBuilder.append(" AND OBJ.clientName like ? ");
			 params.add("%"+ownerName+"%");
		}
		//验房情况 001 ：未验房;(身份验证通过，但还没验房) 002 ：整改中;(已验过房，但没通过，处于整改中) 003 ：验房通过;
		String inspectResult =  queryCondition.getInspectResult();
		if(StringUtils.hasText(inspectResult))
		{
				 hqlBuilder.append(" AND OBJ.inspectResult= ? ");
				 params.add(inspectResult);
		}
		//证 -- 身份验证状态 1 ： 验证通过， 0：验证未通过
		String identityState = queryCondition.getIdentityState();
		if(StringUtils.hasText(identityState))
		{
			 hqlBuilder.append(" AND OBJ.identityState=? ");
			 params.add(identityState);
		}
		//资 -- 文档收发状态 1 ： 已收发， 0：未收发
		String docState = queryCondition.getDocState();
		if(StringUtils.hasText(docState))
		{
			 hqlBuilder.append(" AND OBJ.docState=? ");
			 params.add(docState);
		}
		// 费 -- 预交费用状态 1 ： 已缴， 0：未缴
		String chargeState = queryCondition.getChargeState();
		if(StringUtils.hasText(chargeState))
		{
			 hqlBuilder.append(" AND OBJ.chargeState=? ");
			 params.add(chargeState);
		}
		// 验 -- 验房登记状态 1： 已验房登记， 0：未验房登记
		String inspectCheckinState = queryCondition.getInspectCheckinState();
		if(StringUtils.hasText(inspectCheckinState))
		{
			 hqlBuilder.append(" AND OBJ.inspectCheckinState=? ");
			 params.add(inspectCheckinState);
		}
		// 物 -- 物品移交状态(钥匙移交) 1： 已移交， 0：未移交
		String handoverState = queryCondition.getHandoverState();
		if(StringUtils.hasText(handoverState) )
		{
			 hqlBuilder.append(" AND OBJ.handoverState = ?");
			 params.add(handoverState);
		}
		if(!BlankUtil.isBlank(queryCondition.getOrderKey()) && !BlankUtil.isBlank(queryCondition.getOrderValue()))
		{
			hqlBuilder.append(" ORDER BY ").append("OBJ.").append(queryCondition.getOrderKey()).append(" ").append(queryCondition.getOrderValue());
		}

		String hql = hqlBuilder.toString();
		Object[] paramsObj = params.toArray();
		Page page = this.pagedQueryByPageNo(hql, queryCondition.getPageNo(), queryCondition.getPageSize(),paramsObj);
		if(hql.indexOf("ORDER BY")<0)//如果没有排序
		{
			Collections.sort(page.getResult(), new DOComparator(""));
		}
		//管理房房号
		String coalitionHouseNumber = queryCondition.getCoalitionHouseNumber();
		if(StringUtils.hasText(coalitionHouseNumber))
		{
			List<IdentityValidateDO> result = page.getResult();
			if(result==null || result.isEmpty())
			{
				return page;
			}
			page.setResult(onlyWihtCoalitionHouseNumber(result, coalitionHouseNumber));
		}
		return page;
	}

	private  class DOComparator implements Comparator<IdentityValidateDO>{
		String sort = null;
		public DOComparator(String sort)
		{
			this.sort = sort;
		}

		public int compare(IdentityValidateDO o1, IdentityValidateDO o2)
		{
			long c = 0;
			if(StringUtils.hasText(this.sort))
			{
				if(this.sort.equals("houseId"))
				{
					c = o1.getHouse().getKeyword().longValue() - o2.getHouse().getKeyword().longValue();
				}
			}else{
				c = o1.getHouse().getBuilding().getKeyword().longValue() - o2.getHouse().getBuilding().getKeyword().longValue();
				if(c==0L)
				{
					c = o1.getHouse().getKeyword().longValue() - o2.getHouse().getKeyword().longValue();
				}
			}
			return Integer.parseInt(String.valueOf(c));
		}
	}

	/**
	 * 功能描述: 获取只有含指定管理房房号的对象列表(过滤功能) <br>
	 * @param list
	 * @param coalitionHouseNumber
	 * @return
	 * Create author: 王浩羽 <br>
	 * Create on:Jan 6, 2009  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private List<IdentityValidateDO> onlyWihtCoalitionHouseNumber(List<IdentityValidateDO> list, String coalitionHouseNumber){
		List<IdentityValidateDO> result = new ArrayList<IdentityValidateDO>();
		for(IdentityValidateDO identityValidateDO : list)
		{
			if(isContainsCoalitionHouseNumber(identityValidateDO, coalitionHouseNumber))
			{
				result.add(identityValidateDO);
			}
		}
		return result;
	}
	/**
	 * 功能描述: 判断是否含有指定管理房房号 <br>
	 * @param identityValidateDO
	 * @param coalitionHouseNumber
	 * @return
	 * Create author: 王浩羽 <br>
	 * Create on:Jan 6, 2009  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private boolean isContainsCoalitionHouseNumber(IdentityValidateDO identityValidateDO, String coalitionHouseNumber){
		Set<OccHouseClientInfoDO> clientInfos = identityValidateDO.getClientInfos();
		for(OccHouseClientInfoDO occHouseClientInfoDO : clientInfos)
		{
			if(StringUtils.hasText(occHouseClientInfoDO.getCoalitionHouseNumber()) && occHouseClientInfoDO.getCoalitionHouseNumber().indexOf(coalitionHouseNumber)>-1)
			{
				return true;
			}
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	public IdentityValidateDO findUniqueByHouseId(Long houseId)
	{

		StringBuilder hqlBuilder = new StringBuilder();
		hqlBuilder.append("SELECT OBJ FROM ").append(IdentityValidateDO.class.getSimpleName())
		.append(" AS OBJ WHERE OBJ.house.keyword = ?");
		List<IdentityValidateDO> list =  this.createQuery(hqlBuilder.toString(), houseId).list();
		if(list==null || list.isEmpty())
		{
			return null;
		}
		return list.get(0);
	}

	public boolean isAllowReleaseRealAndVirtual(Long virtualHouseId, List<Long> houseIds)
	{
		Assert.notNull(houseIds);
		Assert.notEmpty(houseIds);
		for(Long houseId : houseIds)
		{
			IdentityValidateDO identityValidateDO = findUniqueByHouseId(houseId);
			if (identityValidateDO==null) {
				continue;
			}
			if(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO.equals(identityValidateDO.getIdentityState()) || SysrefcodeDO.CONSTANTS_DIGITAL_ZERO.equals(identityValidateDO.getHandoverState()))
			{   //如果是没有入伙(认证没有通过、钥匙没有移交)，则不能解除关系
				return false;
			}
			//未收款项目
			List<OccupationChargeItemDO> notChargeItems = occupationChargeItemDao.queryChargeItemByHouseIdAndStatus(houseId,SysrefcodeDO.OM_FEE_STATE_NO);
			if(notChargeItems==null || notChargeItems.isEmpty())
			{
				return false;
			}
		}
		return true;
	}

}
